<?php 
defined('PHPFOX') or exit('NO APPLES!'); 
?>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script>
    setTimeout('document.getElementById("myDiv").style.display="none"', 5000);  // 5 seconds
</script>

<div class="header_bar_menu {if !$user}init{/if}" style="border:1px #DFDFDF solid; background: rgb(248, 248, 248); border-radius: 7px;">
    
	{if !$error}
    <div id="pro_error_container" class="error_message" style="display:none; margin-right: 10px;"></div>
	{else}
	<div id="myDiv" class="error_message" style="margin-right: 10px;">{$error}</div>
	{/if}
    <div class="public_message" {if $success } id="myDiv" style="display:block; margin-right: 10px;" {else} style="display:none" {/if}>{phrase var='profiles.message_has_been_sent'}</div>

    <form method="post" action="{url link='profiles.index'}" id="invite_form">
        <div class="table">
        <!--    <div class="table_left go_left" style="width: 200px;">
				{phrase var='profiles.search_member'}: 
            </div> -->
            <div class="table_right go_left" style="width: 81%;">
                {if $user }
                    <input tabindex="1" name="val[username]" readonly="" id="pro_username" size="40" type="text" value="{$user.full_name}" placeholder="{phrase var='profiles.search_member'}">
                {else}
                    <input tabindex="1" name="val[username]" id="pro_username" size="40" type="text" value="{$aVals.username}" placeholder="{phrase var='profiles.search_member'}">
                {/if}
            </div>
            <div class="clear"></div>
        </div>
		<input class="clock" type="button">
		<div class="table" id="pro_email" style=" {if $user.not_friend || $aVals.email }  display: block;{else} display:none; {/if}">
         <!--   <div class="table_left go_left" style="width: 200px;">
                {phrase var='profiles.email'}:
            </div> -->
            <div class="table_right go_left" style="width: 81%;">
                <input name="val[email]" id="email" size="40" type="text" value="{$aVals.email}" placeholder="{phrase var='profiles.email'}">
            </div>
            <div class="clear"></div>
        </div>
        <div class="table">
			<div class="table_left go_left" style="width: 200px;">
                {phrase var='profiles.message'}:
            </div>
            <div class="table_right go_left" style="width: 81%;">
                <textarea name="val[message]" id="message"  placeholder="What are you thinking about?">{$aVals.message}</textarea>
            </div>
            <input type="hidden" id="user_id" name="val[user_id]" value="{if $aVals.user_id}{$aVals.user_id}{elseif $user}{$user.user_id}{/if}" >
            <input type="hidden" id="type" name="val[type]" value="{$aVals.type}" >
            <div class="clear"></div>
        </div>
        <div class="table_clear">
			<input class="reset" type="button" value="Reset" onClick="history.go(0)">
            <input class="button" type="submit" value="{phrase var='profiles.send'}">
        </div>
    </form>
    <input type="hidden" id="ostranslate_search_name" value="{phrase var='profiles.searchname_is_required'}">
    <input type="hidden" id="ostranslate_email" value="{phrase var='profiles.email_is_required'}">
    <input type="hidden" id="ostranslate_message" value="{phrase var='profiles.message_is_required'}">
    <input type="hidden" id="server_base_url" value="{url link='profiles' }">
    <input type="hidden" id="clicktype" value="">
</div>
<style>
    {literal}
        #invite_form .table_right input{
            width:100%;
			background: #E0ECDC;
        }
        #invite_form .table_right textarea{
            width:100%;
            height:47px;
			background: #E0ECDC;
        }
        #invite_form .table_clear{
            text-align: right;
			margin-top: -72px;
			padding: 5px 10px 5px 0px;
		}
		.clock{ background:url(../module/profiles/static/image/default/default/time_clock.png) no-repeat;
		float: right;
		width:30px;
		height:30px;
		border:0 none;
		margin: -39px 29px 0px 0px;
		cursor:pointer;
		}
		.reset{
		color: #fef4e9;
		border: solid 1px #da7c0c;
		font-size: 11px;
		padding: 4px 6px 4px 6px;
		margin: 0px 10px 5px 0px;
		font-weight: bold;
		background: #f78d1d;
		background: -webkit-gradient(linear, left top, left bottom, from(#faa51a), to(#f47a20));
		background: -moz-linear-gradient(top,  #faa51a,  #f47a20);
		filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#faa51a', endColorstr='#f47a20');
		cursor:pointer;
		}
		.reset:hover {
		background: #f47c20;
		background: -webkit-gradient(linear, left top, left bottom, from(#f88e11), to(#f06015));
		background: -moz-linear-gradient(top,  #f88e11,  #f06015);
		filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#f88e11', endColorstr='#f06015');
		}
		.reset:active {
		color: #fcd3a5;
		background: -webkit-gradient(linear, left top, left bottom, from(#f47a20), to(#faa51a));
		background: -moz-linear-gradient(top,  #f47a20,  #faa51a);
		filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#f47a20', endColorstr='#faa51a');
		}
    {/literal}
</style>
 <script>
  {literal}
//    $(function() {
    function clear_sub(){
        $("#user_id").val('');
        $("#type").val('');
        $("#pro_email").hide();
        $("#email").val('');
    }
    function clear_all(){
        $("#message").val('');
        $("#pro_username").val('');
        $("#user_id").val('');
        $("#type").val('');
        $("#pro_email").hide();
        $("#email").val('');
    }
    $Core.ajax1 = function(sCall, $oParams)
    {
        var sParams = '&' + getParam('sGlobalTokenName') + '[ajax]=true&' + getParam('sGlobalTokenName') + '[call]=' + sCall;
        
        if (!sParams.match(/\[security_token\]/i))
        {
            sParams += '&' + getParam('sGlobalTokenName') + '[security_token]=' + oCore['log.security_token'];
        }
        
        if (isset($oParams['params']))
        {
            if (typeof($oParams['params']) == 'string')
            {
                sParams += $oParams['params'];
            }
            else        
            {
                $.each($oParams['params'], function($sKey, $sValue)
                {
                    sParams += '&' + $sKey + '=' + encodeURIComponent($sValue) + '';
                });
            }        
        }
        
        $.ajax(
        {
            type: (isset($oParams['type']) ? $oParams['type'] : 'GET'),
            url: getParam('sJsStatic') + "ajax.php",
            dataType: 'json',
            data: sParams,
            success: $oParams['success']
        });    
    };
//    $Behavior.autocomplete = function(){
        $(".init #pro_username").blur(function(){
            setTimeout(function(){
                if($("#clicktype").val() != '1'){
                    $("#pro_username").val('');
                    clear_sub();
                }
            },1000)
        });
        $(".init #pro_username").keypress(function(){
            clear_sub();
        });
        $(".init #pro_username").autocomplete({
            source: "static/ajax.php?core[ajax]=true&core[call]=profiles.getUserNames&core[security_token]="+$("[name='core[security_token]']").val(),
            select: function(event, ui) {
                var base_url = $("#server_base_url").val();
                $("#type").val(ui.item.type);
                $("#clicktype").val('1');
                $("#pro_username").val(ui.item.id);
                $Core.ajax1("profiles.getInfoUser",{
                    type : "POST",
                    dataType : "json",
                    params : {"username":ui.item.id},
                    success : function($data){
                        switch($data.message){
                            case "many_users":
                                document.location.href = base_url + 'member/user_'+ui.item.id;
                            break;
                            case "no_friend":
                                $("#pro_email").show();
                                $('#type').val('1');
                                $('#email').focus();
                                $("#user_id").val($data.user_id);
                            break;
                            case "friend":
                                $("#pro_email").hide();
                                $('#type').val('0');
                                $('#message').focus();
                                $("#user_id").val($data.user_id);
                            break;
                            case "no_users":
                                $("#pro_email").show();
                                $("#user_id").val("-1");
                                $('#type').val('2');
                                $('#email').focus();
                                return false;
                            break;
                        }
                    }
                })
                return false;
//                if(ui.item.id.indexOf('re_') != -1){
//                    $("#pro_username").val(ui.item.id.split('re_')[1]);
//                    $("#pro_email").show();
//                    $("#user_id").val("");
//                    $('#email').focus();
//                    return false;
//                }else{
//                    if(ui.item.not_friend == 1){
//                        $("#pro_email").show();
//                        $('#email').focus();
//                    }else{
//                        $("#pro_email").hide();
//                        $('#message').focus();
//                    }
//                    $("#user_id").val(ui.item.id);
//                }
//                $Core.loadInit();
            }
        });
        $("#invite_form").submit(function(){
            var message = ""
            console.log(oTranslations)
            if(!$("#pro_username").val()){
                message += "<p>"+$("#ostranslate_search_name").val()+"</p>";
            }
            if(!$("#pro_email").is(':hidden')) { 
                if(!$("#email").val()){
                    message += "<p>"+$("#ostranslate_email").val()+"</p>";
                }
            }
            if(!$("#message").val()){
                message += "<p>"+$("#ostranslate_message").val()+"</p>";
            }
            if(message){
                $("#pro_error_container").html(message);
                $("#pro_error_container").show();
				setTimeout('document.getElementById("pro_error_container").style.display="none"', 6000);  // 6 seconds
                return false;
            }
        })
//   };
//   $Behavior.autocomplete();
  {/literal}
</script>
